package com.scan.bus;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.shunlian.mylibrary.BarHide;
import com.shunlian.mylibrary.ImmersionBar;

import java.util.List;

/**
 * Created by zhang on 2017/4/13 16 : 37.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener{
    public ImmersionBar immersionBar;
    private Resources resources;
    protected Activity baseAct;
    private InputMethodManager mInputMethodManager;
    public static boolean isNetwork = false;
    protected static Handler handler=new Handler();


    @Override
    public void onBackPressed() {
        // 这里处理逻辑代码，大家注意：该方法仅适用于2.0或更新版的sdk
        //部分新版android机有侧滑返回，但是不走onKeyDown物理键,因此监听返回统一用此方法
        super.onBackPressed();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseAct=this;
        immersionBar = ImmersionBar.with(this);
        immersionBar.init();
        setContentView(getLayoutView());
        resources = getResources();
        initListener();
        initData();
    }


    @Override
    protected void onRestart() {
        super.onRestart();
    }



    public Handler handlerLoad=new Handler();


    @Override
    protected void onResume() {
        super.onResume();
    }



    /**
     * 布局view
     *
     * @return
     */
    protected abstract View getLayoutView();

    /**
     * 初始化监听事件，如果需要监听重新即可
     */
    protected void initListener() {
    }

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 设置状态栏的颜色
     *
     * @param statusBarColor
     */
    public void setStatusBarColor(@ColorRes int statusBarColor) {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        ImmersionBar
                immersionBar = this.immersionBar.fitsSystemWindows(true).statusBarColor(statusBarColor);
        if (statusBarColor == R.color.white){
            immersionBar.navigationBarColor(R.color.white_ash);
            immersionBar.navigationBarDarkIcon(true);
        }else {
            immersionBar.navigationBarDarkIcon(false);
        }
        immersionBar.init();
    }

    /**
     * 设置状态栏的颜色和底部导航颜色
     * @param statusBarColor
     * @param naigationColor
     */
    public void setStatusNaigationBarColor(@ColorRes int statusBarColor,@ColorRes int naigationColor) {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        if (statusBarColor == R.color.white || statusBarColor == R.color.white_ash){
            immersionBar.navigationBarDarkIcon(true);
        }else {
            immersionBar.navigationBarDarkIcon(false);
        }
        immersionBar.fitsSystemWindows(true).statusBarColor(statusBarColor)
                .navigationBarColor(naigationColor).init();
    }

    /**
     * 设置状态栏的颜色和底部导航颜色
     * @param statusBarColor
     * @param naigationColor
     */
    public void setStatusNaigationBarColor(@ColorRes int statusBarColor,String naigationColor) {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        if (statusBarColor == R.color.white || statusBarColor == R.color.white_ash){
            immersionBar.navigationBarDarkIcon(true);
        }else {
            immersionBar.navigationBarDarkIcon(false);
        }
        immersionBar.fitsSystemWindows(true).statusBarColor(statusBarColor)
                .navigationBarColor(naigationColor).init();
    }

    /**
     *设置状态栏高度，解决重叠
     * @param statusBarID
     */
    public void setStatusBarView(@IdRes int statusBarID) {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.statusBarView(statusBarID).init();
    }

    /**
     * 当状态栏颜色为白色
     * 设置状态栏字体的颜色，仅限于魅族flymeos4以上，小米MIUI6以上，Android6.0以上
     * 如果不支持修改颜色就给状态栏设置为透明度
     */
    public void setStatusBarFontDark() {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.statusBarDarkFont(true, 0.2f).init();
    }

    /**
     * @param isFullScreen 设置全屏显示,true全屏
     */
    public void setFullScreen(boolean isFullScreen){
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.fullScreen(isFullScreen).init();
    }

    /**
     * 隐藏状态栏
     */
    public void setHideStatus() {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();
    }

    /**
     * 如果此设备有导航栏的话，调用该方法隐藏导航栏
     */
    public void setHideNavigation() {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        if (ImmersionBar.hasNavigationBar(this))
            immersionBar.hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init();
    }

    /**
     * 隐藏导航栏和状态栏
     */
    public void setHideStatusAndNavigation() {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.hideBar(BarHide.FLAG_HIDE_BAR).init();
    }

    /**
     * 恢复显示导航栏和状态栏
     */
    public void setShowStatusAndNavigation() {
        if (immersionBar == null){
            immersionBar = ImmersionBar.with(this);
        }
        immersionBar.hideBar(BarHide.FLAG_SHOW_BAR).init();
    }

    /**
     * 显示view
     *
     * @param views
     */
    protected void visible(View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }
    }
    /**
     * 显示view
     *
     * @param views
     */
    protected void invisible(View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.INVISIBLE);
                }
            }
        }
    }

    /**
     * 隐藏view
     *
     * @param views
     */
    protected void gone(View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }

    protected void setEdittextFocusable(boolean focusable,EditText... editText){
        for (int i = 0; i < editText.length; i++) {
            editText[i].setFocusable(focusable);
            editText[i].setFocusableInTouchMode(focusable);
            if (focusable){
                editText[i].requestFocus();
            }
        }
    }

    /**
     * 获取文字资源
     *
     * @param stringResouce
     */
    protected String getStringResouce(int stringResouce) {
        return resources.getString(stringResouce);
    }

    /**
     * 获取颜色资源
     *
     *  @param colorResouce
     */
    protected int getColorResouce(int colorResouce) {
        return resources.getColor(colorResouce);
    }

    /**
     * 获取图片资源
     *
     *  @param drawableResouce
     */
    protected Drawable getDrawableResouce(int drawableResouce) {
        return resources.getDrawable(drawableResouce);
    }

    /**
     * 判断集合内容是否为空
     * @param list
     * @return
     */
    protected boolean isEmpty(List list){
        if (list == null){
            return true;
        }

        if (list.size() == 0){
            return true;
        }else {
            return false;
        }
    }

    protected boolean isEmpty(CharSequence sequence){
        return TextUtils.isEmpty(sequence);
    }

    @Override
    protected void onStop() {
        super.onStop();
//        dismissDialog(false);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        if (immersionBar != null){
            immersionBar.destroy();
        }
    }

    @Override
    public void onClick(View view) {
        mOnClick(view);
    }
    public void mOnClick(View view){

    }

    @Override
    public void finish() {
        super.finish();
        hideSoftKeyBoard();
    }


    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.mInputMethodManager == null) {
            this.mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.mInputMethodManager != null)) {
            this.mInputMethodManager.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f){
            Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration,resources.getDisplayMetrics());
        }
        return resources;
    }

}
